package cn.jiyun.provider.controller;

import cn.jiyun.provider.dao.UserDao;
import cn.jiyun.provider.pojo.Dept;
import cn.jiyun.provider.pojo.User;
import cn.jiyun.provider.pojo.UserDto;
import cn.jiyun.provider.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/page")
    @CrossOrigin
    public Page page(@RequestParam(defaultValue = "1") Integer current,@RequestParam("name") String name){
        return userService.page(current,name);
    }
    @RequestMapping("/add")
    @CrossOrigin
    public Integer add(@RequestBody User user){
        int i=userService.add(user);
        return i;
    }
    @RequestMapping("/getById")
    @CrossOrigin
    public User getById(Long id){
        return userService.getById(id);
    }
    @RequestMapping("/edit")
    @CrossOrigin
    public Integer edit(@RequestBody User user){
        int i=userService.edit(user);
        return i;
    }
    @RequestMapping("/del")
    @CrossOrigin
    public Integer del(Long id){
        int i=userService.del(id);
        return i;
    }
    @RequestMapping("/excelFile")
    @CrossOrigin
    public void excelFile(HttpServletResponse response){
      userService.excelFile(response);
    }
    @RequestMapping("/getDept")
    @CrossOrigin
    public List<Dept> getDept(){
      return userService.depts();
    }
   /* @RequestMapping("/upload")
    @CrossOrigin
    public String upload(MultipartFile imgFile, HttpServletRequest request) throws IOException {
        String realPath = request.getSession().getServletContext().getRealPath("/");
        String filename = imgFile.getOriginalFilename();
        imgFile.transferTo(new File(realPath+"/"+filename));
        String avatar="../"+filename;
        return avatar;
    }*/

}
